package org.ne.springboot.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.ne.springboot.interceptor.MinioService;
import org.ne.springboot.pojo.MinioPojo;
import org.ne.springboot.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author wey
 * @date 2025/1/17 下午10:34 1月
 */


@RestController
@RequestMapping("/file")
public class FileUploadController {

//    @PostMapping("/upload")
//    public Result upload(MultipartFile file) throws Exception {
//        String fileName = file.getOriginalFilename();
//
//        fileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
//
//
////        file.transferTo(new File("D:\\uploads\\"+fileName));
//        Object  result = readWordText(fileName, file.getInputStream());
////        String url = OSSUtils.uploadFile(fileName ,file.getInputStream() );
//        Object res = extractTitles(file.getInputStream());
//        Map<String, Object> map = new HashMap<>();
//        map.put("content", result);
//        map.put("titles", res);
//        return Result.success(map);
//    }


    @Autowired
    private MinioService minioService;
    @Autowired
    private MinioPojo minioPojo;


    //文件上传
//    @Operation(summary = "上传图片")
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file){
        String url = minioService.uploadFile(file);
        return Result.success(url);
    }


//    @GetMapping("/download")
    @GetMapping(value = "/download")
    public void downloadFile(HttpServletResponse response, @RequestParam("url") String url) throws IOException {
        try {
            minioService.getObject(response, url);
        } catch (Exception e) {
            // 异常处理：返回错误信息（需避免冲突）
            response.reset(); // 重置响应
            response.setContentType("application/json");
            response.getWriter().write("{\"code\":500,\"msg\":\"文件下载失败\"}");
        }
    }

    @PostMapping("/delete")

    public Result deleteFile( @RequestParam("url")  String url){
        minioService.deleteFile(url);
        return Result.success(url);
    }

}


